using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class CurrencyExchangeRatesRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class CurrencyExchangeRatesDbo : DatabaseObject
        {
            public CurrencyExchangeRatesDbo()
            {
                Name = "CurrencyExchangeRates";
                GeneratedClassName = "CurrencyExchangeRates";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CountryId", ColumnType = "smallint", IsIdentity = false, IsNullable = false, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "CountryId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Date", ColumnType = "date", IsIdentity = false, IsNullable = false, Length = 3, Precision = 10, Scale = 0, GeneratedPropertyName = "Date" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Rate", ColumnType = "decimal", IsIdentity = false, IsNullable = false, Length = 9, Precision = 18, Scale = 8, GeneratedPropertyName = "Rate" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SourceCurrencyId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "SourceCurrencyId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "TargetCurrencyId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "TargetCurrencyId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ForexRate", ColumnType = "decimal", IsIdentity = false, IsNullable = true, Length = 9, Precision = 18, Scale = 8, GeneratedPropertyName = "ForexRate" });
              
            }
        }
		
		public static List<CurrencyExchangeRateRow> Select(Expression<Func<CurrencyExchangeRateRow, bool>> expression)
        {
            List<CurrencyExchangeRateRow> rows = new List<CurrencyExchangeRateRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<CurrencyExchangeRateRow>(new CurrencyExchangeRatesDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<CurrencyExchangeRateRow> Select()
		{
			List<CurrencyExchangeRateRow> rows = new List<CurrencyExchangeRateRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<CurrencyExchangeRateRow>(new CurrencyExchangeRatesDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<CurrencyExchangeRateRow> PagedSelect(Expression<Func<CurrencyExchangeRateRow, bool>> filterExpression, Expression<Func<CurrencyExchangeRateRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<CurrencyExchangeRateRow> rows = new List<CurrencyExchangeRateRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<CurrencyExchangeRateRow>(new CurrencyExchangeRatesDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<CurrencyExchangeRateRow> CurrencyExchangeRateList)
        {
            CurrencyExchangeRateRow row;

            while (reader.Read())
            {
                row = new CurrencyExchangeRateRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					CountryId = Util.CastDbValue<int>(reader["CountryId"]),
					Date = Util.CastDbValue<DateTime>(reader["Date"]),
					Rate = Util.CastDbValue<decimal>(reader["Rate"]),
					SourceCurrencyId = Util.CastDbValue<int>(reader["SourceCurrencyId"]),
					TargetCurrencyId = Util.CastDbValue<int>(reader["TargetCurrencyId"]),
					ForexRate = Util.CastDbValue<decimal?>(reader["ForexRate"])
                };
                                
                CurrencyExchangeRateList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(CurrencyExchangeRateRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<CurrencyExchangeRateRow>(new CurrencyExchangeRatesDbo(), row);
            }
        }

		public static void Update(CurrencyExchangeRateRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<CurrencyExchangeRateRow>(new CurrencyExchangeRatesDbo(), row);
            }
        }

		public static void Delete(Expression<Func<CurrencyExchangeRateRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<CurrencyExchangeRateRow>(new CurrencyExchangeRatesDbo(), expression);
            }
        }

	}
}
	
